package net.zigin.nhi.could.waste.rabbitmq;

import net.zigin.nhi.could.waste.rabbitmq.model.MessageModel;
import net.zigin.nhi.could.waste.config.RabbitMqConfig;
import net.zigin.nhi.could.waste.service.WarnTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import com.rabbitmq.client.Channel;

@Component
public class Consumers {

    @Autowired
    private WarnTimeService warnTimeService;

    @RabbitListener(queues = RabbitMqConfig.deal_queue)
    public void process(MessageModel order, Message message, @Headers Map<String, Object> headers, Channel channel) throws IOException {
        System.out.println(order);
        handler(headers, channel);
    }

    @RabbitListener(queues = RabbitMqConfig.dcrdq)
    public void process1(MessageModel order, Message message, @Headers Map<String, Object> headers, Channel channel) throws IOException {
        warnTimeService.generateCollectWarn(order);
        System.out.println("consumer接收到"+order.getTitle()+"的请求并处理完毕，时间是"+new Date());
        handler(headers, channel);
    }

    @RabbitListener(queues = RabbitMqConfig.idrdq)
    public void process2(MessageModel order, Message message, @Headers Map<String, Object> headers, Channel channel) throws IOException {
        warnTimeService.generateWarn(order);
        System.out.println("consumer接收到"+order.getTitle()+"的请求并处理完毕，时间是"+new Date());
        handler(headers, channel);
    }

    @RabbitListener(queues = RabbitMqConfig.odrdq)
    public void process3(MessageModel order, Message message, @Headers Map<String, Object> headers, Channel channel) throws IOException {
        warnTimeService.generateWarn(order);
        System.out.println("consumer接收到"+order.getTitle()+"的请求并处理完毕，时间是"+new Date());
        handler(headers, channel);
    }

    @RabbitListener(queues = RabbitMqConfig.dcydq)
    public void process4(MessageModel order, Message message, @Headers Map<String, Object> headers, Channel channel) throws IOException {
        warnTimeService.generateCollectWarn(order);
        System.out.println("consumer接收到"+order.getTitle()+"的请求并处理完毕，时间是"+new Date());
        handler(headers, channel);
    }

    @RabbitListener(queues = RabbitMqConfig.idydq)
    public void process5(MessageModel order, Message message, @Headers Map<String, Object> headers, Channel channel) throws IOException {
        warnTimeService.generateWarn(order);
        System.out.println("consumer接收到"+order.getTitle()+"的请求并处理完毕，时间是"+new Date());
        handler(headers, channel);
    }

    @RabbitListener(queues = RabbitMqConfig.odydq)
    public void process6(MessageModel order, Message message, @Headers Map<String, Object> headers, Channel channel) throws IOException {
        warnTimeService.generateWarn(order);
        System.out.println("consumer接收到"+order.getTitle()+"的请求并处理完毕，时间是"+new Date());
        handler(headers, channel);
    }

    private void handler(Map<String, Object> headers, Channel channel) {
        // 手动ack
        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        // 手动签收
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("执行结束....");
    }


}
